package com.homework;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/** HClient01
 * 作业1
 * 客户端
 */
public class HClient01 {
    public static void main(String[] args) throws IOException {
        //连接服务端
        Socket socket=new Socket(InetAddress.getLocalHost(),6666);
        //发送消息
        char[] buf=new char[1024];
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        System.out.println("请输入问题:name or hobby");
        Scanner scanner=new Scanner(System.in);
        String qes=scanner.next();

        bufferedWriter.write(qes);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        //客户端接收消息 并打印出
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String mess=bufferedReader.readLine();
        System.out.println(mess);
        //关闭流
        bufferedWriter.close();
        bufferedReader.close();
        socket.close();

    }

    public static void error() throws IOException {
        //连接服务端
        Socket socket=new Socket(InetAddress.getLocalHost(),6666);
        //发送消息
        char[] buf=new char[1024];
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        String name="name";
        bufferedWriter.write(name);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        // socket.shutdownOutput();

        String hobby="hobby";
        bufferedWriter.write(hobby);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        //socket.shutdownOutput();

        //客户端接收消息 并打印出
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int len=0;
        char[]message=new char[1024];
        while((len=bufferedReader.read(message))!=-1){
            System.out.println(new String(message,0,len));
        }

        //关闭流
        bufferedWriter.close();
        bufferedReader.close();
        socket.close();
    }
}
